﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace GTTSOfLibrary.Draw
{
    /// <summary>
    /// 点所对应的矩形区域
    /// </summary>
    [Serializable]
    public class CustomRectangle
    {
        public CustomRectangle()
        { }

        public CustomRectangle(float x, float y, float Width, float Height)
        {
            this.X = x;
            this.Y = y;
            this.Width = Width;
            this.Height = Height;
        }

        #region 属性
        /// <summary>
        /// X坐标
        /// </summary>
        public float X { get; set; }
        /// <summary>
        /// Y坐标
        /// </summary>
        public float Y { get; set; }
        /// <summary>
        /// X坐标
        /// </summary>
        public float Left
        {
            get { return X; }
            set { X = value; }
        }
        /// <summary>
        /// Y坐标
        /// </summary>
        public float Top
        {
            get { return Y; }
            set { Y = value; }
        }
        /// <summary>
        /// 宽度
        /// </summary>
        public float Width { get; set; }
        /// <summary>
        /// 高度
        /// </summary>
        public float Height { get; set; }

        /// <summary>
        /// 返回右边X轴坐标（等于X+Width）
        /// </summary>
        public float Right
        {
            get { return X + Width; }
        }
        /// <summary>
        /// 返回底部Y轴坐标（等于Y+Height）
        /// </summary>
        public float Bottom
        {
            get { return Y + Height; }
        }
        /// <summary>
        /// 返回矩形中点
        /// </summary>
        public CustomPoint CenterPoint
        {
            get { return new CustomPoint(X + Width / 2, Y + Height / 2); }
        }
        #endregion

        #region 方法区
        /// <summary>
        /// 返回一个克隆的对象
        /// </summary>
        /// <returns></returns>
        public CustomRectangle Clone()
        {
            return new CustomRectangle(X, Y, Width, Height);
        }


        /// <summary>
        /// 转化为Rectangle对象
        /// </summary>
        /// <returns></returns>
        public Rectangle ToRectangle()
        {
            return new Rectangle((int)X, (int)Y, (int)Width, (int)Height);
        }

        /// <summary>
        /// 转化为RectangleF对象
        /// </summary>
        /// <returns></returns>
        public RectangleF ToRectangleF()
        {
            return new RectangleF(X, Y, Width, Height);
        }

        /// <summary>
        /// 返回一个左上角坐标点
        /// </summary>
        /// <returns></returns>
        public Point ToPoint()
        {
            return new Point((int)X, (int)Y);
        }

        /// <summary>
        /// 返回一个左上角坐标点
        /// </summary>
        /// <returns></returns>
        public PointF ToPointF()
        {
            return new PointF(X, Y);
        }

        /// <summary>
        /// 判断指定坐标的点是否在矩形内部
        /// </summary>
        /// <param name="ptX">X坐标</param>
        /// <param name="ptY">Y坐标</param>
        /// <returns></returns>
        public bool IsPointInRectangle(int ptX, int ptY)
        {
            return (ptX >= X
                    && ptX <= (X + Width)
                    && ptY >= Y
                    && ptY <= (Y + Height));
        }

        /// <summary>
        /// 判断指定坐标的点是否在矩形内部
        /// </summary>
        /// <param name="ptX">X坐标</param>
        /// <param name="ptY">Y坐标</param>
        /// <returns></returns>
        public bool IsPointFInRectangle(float ptX, float ptY)
        {
            return (ptX >= X
                    && ptX <= (X + Width)
                    && ptY >= Y
                    && ptY <= (Y + Height));
        }

        /// <summary>
        /// 将RectangleF转化为CustomRectangle
        /// </summary>
        /// <param name="re">要转换的RectangleF</param>
        /// <returns></returns>
        public static CustomRectangle ToCustomRectangle(RectangleF re)
        {
            CustomRectangle cus = new CustomRectangle();
            cus.X = re.X;
            cus.Y = re.Y;
            cus.Width = re.Width;
            cus.Height = re.Height;
            return cus;
        }

        /// <summary>
        /// 根据Rectangle产生一个CustomRectangle对象
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static CustomRectangle FromRectangle(Rectangle rect)
        {
            return new CustomRectangle(rect.Left, rect.Top, rect.Width, rect.Height);
        }

        /// <summary>
        /// 根据RectangleF产生一个CustomRectangle对象
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static CustomRectangle FromRectangleF(RectangleF rect)
        {
            return new CustomRectangle(rect.Left, rect.Top, rect.Width, rect.Height);
        }

        /// <summary>
        /// 获取边框的圆角矩形路径（用于绘图）
        /// </summary>
        /// <returns></returns>
        public GraphicsPath GetRoundRectBorderPath(float radus)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddArc(X, Y, radus * 2, radus * 2, 180, 90);
            path.AddArc(Right - radus * 2, Y, radus * 2, radus * 2, 270, 90);
            path.AddArc(Right - radus * 2, Bottom - radus * 2, radus * 2, radus * 2, 0, 90);
            path.AddArc(X, Bottom - radus * 2, radus * 2, radus * 2, 90, 90);
            path.CloseFigure();
            return path;
        }

        /// <summary>
        /// 获取六边形绘图路径
        /// </summary>
        /// <returns></returns>
        public GraphicsPath GetHexagonBorderPath()
        {
            GraphicsPath path = new GraphicsPath();
            List<PointF> pts = new List<PointF>();
            pts.Add(new PointF(X + Width / 2f, Y));
            pts.Add(new PointF(Right, Y + Height / 4f));
            pts.Add(new PointF(Right, Y + Height * 3f / 4f));
            pts.Add(new PointF(X + Width / 2f, Bottom));
            pts.Add(new PointF(X, Y + Height * 3f / 4f));
            pts.Add(new PointF(X, Y + Height / 4f));
            path.AddPolygon(pts.ToArray());
            return path;
        }

        /// <summary>
        /// 获取倒三角形路径
        /// </summary>
        /// <returns></returns>
        public GraphicsPath GetTrianglePath()
        {
            GraphicsPath path = new GraphicsPath();
            List<PointF> pts = new List<PointF>();
            pts.Add(new PointF(X, Y));
            pts.Add(new PointF(Right, Y));
            pts.Add(new PointF(X + Width / 2f, Bottom));
            path.AddPolygon(pts.ToArray());
            return path;
        }

        /// <summary>
        /// 根据鼠标位置确定高亮区域，1、2、3、4表示中间4个区域，其他为-1
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public int GetHotPosition(Point pt)
        {
            Point pt1, pt2, pt3, pt4, pt0;
            pt1 = new Point((int)(X + Width / 6), (int)(Y + Height / 6));
            pt2 = new Point((int)(Right - Width / 6), (int)(Y + Height / 6));
            pt3 = new Point((int)(Right - Width / 6), (int)(Bottom - Height / 6));
            pt4 = new Point((int)(X + Width / 6), (int)(Bottom - Height / 6));
            pt0 = CenterPoint.ToPoint();

            GraphicsPath path1 = new GraphicsPath();
            path1.AddPolygon(new Point[] { pt1, pt2, pt0 });
            if (path1.IsVisible(pt)) return 1;

            GraphicsPath path2 = new GraphicsPath();
            path2.AddPolygon(new Point[] { pt2, pt3, pt0 });
            if (path2.IsVisible(pt)) return 2;

            GraphicsPath path3 = new GraphicsPath();
            path3.AddPolygon(new Point[] { pt3, pt4, pt0 });
            if (path3.IsVisible(pt)) return 3;

            GraphicsPath path4 = new GraphicsPath();
            path4.AddPolygon(new Point[] { pt4, pt1, pt0 });
            if (path4.IsVisible(pt)) return 4;

            return -1;
        }

        /// <summary>
        /// 获取高亮矩形区域
        /// </summary>
        /// <param name="hotPosition">1,2,3,4；0表示全选</param>
        /// <returns></returns>
        public Rectangle GetSubRectangle(int hotPosition)
        {
            if (hotPosition == 1)
                return new Rectangle((int)X, (int)Y, (int)Width, (int)(Height / 2));
            else if (hotPosition == 2)
                return new Rectangle((int)(X + Width / 2), (int)Y, (int)(Width / 2), (int)Height);
            else if (hotPosition == 3)
                return new Rectangle((int)X, (int)(Y + Height / 2), (int)Width, (int)(Height / 2));
            else if (hotPosition == 4)
                return new Rectangle((int)X, (int)Y, (int)(Width / 2), (int)Height);
            else
                return ToRectangle();
        }

        #endregion
    }
}
